package com.ihealthway.all.groupbuy.modules.admin;

import com.ihealthway.all.groupbuy.services.CompanyService;
import com.ihealthway.all.slog.annotation.SLog;
import com.ihealthway.all.commons.Constants;
import com.ihealthway.all.utils.Ajax;
import com.ihealthway.all.utils.AjaxReturn;
import com.ihealthway.all.groupbuy.vo.Company;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import java.util.List;
import java.util.Map;

/**
 * Company module class
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean
@At("/admin/company")
public class CompanyModule {

    private static final Log logger = Logs.get();

    @Inject
    private CompanyService companyService;

    @At
    @Ok("beetl:admin/company.jsp")
    public void index() {

    }

    @At
    @POST
    public AjaxReturn checkExist(@Param("cUUId") String companyUUId) {
        AjaxReturn ar = Ajax.ok();

        boolean flag = companyService.getExist(companyUUId);
        ar.setOk(flag);
        if (flag) {
            ar.setMsg("已经存在相同ID的公司信息.");
        }

        return ar;
    }

    @At
    @POST
    @SLog(module = Constants.MODULE_SYSTEM, type = Constants.OPERATE_TYPE_ADD,
            content = "新增【${args[0].companyName}】公司信息")
    public AjaxReturn addNew(@Param("..") Company company) {

        AjaxReturn ar = Ajax.ok();

        boolean flag = companyService.add(company);
        ar.setOk(flag);
        if (!flag) {
            ar.setMsg("新增公司信息失败,请联系管理员.");
        }

        return ar;
    }

    @At
    @POST
    @SLog(module = Constants.MODULE_SYSTEM, type = Constants.OPERATE_TYPE_UPDATE,
            content = "更新【${args[0].companyName}】公司信息")
    public AjaxReturn update(@Param("..") Company company) {

        AjaxReturn ar = Ajax.ok();

        boolean flag = companyService.update(company);
        ar.setOk(flag);
        if (!flag) {
            ar.setMsg("更新公司信息失败,请联系管理员.");
        }

        return ar;
    }

    @At
    @POST
    @SLog(module = Constants.MODULE_SYSTEM, type = Constants.OPERATE_TYPE_DELETE, content = "删除【${args[0].companyName}】公司信息")
    public AjaxReturn delete(@Param("..") Company company) {

        AjaxReturn ar = Ajax.ok();

        company.setDataStatus("1");
        boolean flag = companyService.update(company);
        ar.setOk(flag);
        if (!flag) {
            ar.setMsg("删除公司信息失败,请联系管理员.");
        }

        return ar;
    }

    @POST
    @At
    public Map<String,Object> dataList(int page, int rows) {

        Map<String, Object> map = companyService.easyuiDGPager(page, rows, null);

        return map;
    }

    @POST
    @At
    public List<Company> findCompany() {

        List<Company> companies = companyService.getCompany();

        return companies;
    }
}
